//
//  SISSPropertyStateCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/9/29.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractPropertyStateCell.h"
#import "SISSTextView.h"


@interface SISSContractPropertyStateCell ()<UITextFieldDelegate,UITextViewDelegate>

/// checkOneButton
@property (weak, nonatomic) UIButton *checkOneButton;
/// checkOneButton
@property (weak, nonatomic) UIButton *checkTwoButton;
/// checkThreeButton
@property (weak, nonatomic) UIButton *checkThreeButton;
/// checkFourButton
@property (weak, nonatomic) UIButton *checkFourButton;

/// oneLabel
@property (weak, nonatomic) YYLabel *oneLabel;
/// twoLabel
@property (weak, nonatomic) YYLabel *twoLabel;
/// threeLabel
@property (weak, nonatomic) YYLabel *threeLabel;


/// oneTextField
@property (weak, nonatomic) UITextField *oneTextField;
/// twoTextField
@property (weak, nonatomic) UITextField *twoTextField;
/// threeTextField
@property (weak, nonatomic) UITextField *threeTextField;
/// threeSecondTextField
@property (weak, nonatomic) UITextField *threeSecondTextField;

/* oneLine */
@property (weak, nonatomic) UIView *oneLine;
/* twoLine */
@property (weak, nonatomic) UIView *twoLine;
/* threeLine */
@property (weak, nonatomic) UIView *threeLine;

/// textView
@property (weak, nonatomic) SISSTextView *textView;

/// checkedButton
@property (weak, nonatomic) UIButton *checkedButton;

@end
@implementation SISSContractPropertyStateCell

- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    if (contractModel.firstIndex == 1) {
        self.checkedButton = self.checkOneButton;
        self.oneTextField.text = contractModel.rightStr;
        self.oneTextField.userInteractionEnabled = YES;
    } else if (contractModel.firstIndex == 2) {
        self.checkedButton = self.checkTwoButton;
        self.twoTextField.text = contractModel.rightStr;
        self.twoTextField.userInteractionEnabled = YES;
    } else if (contractModel.firstIndex == 3) {
        self.checkedButton = self.checkThreeButton;
        self.threeTextField.text = contractModel.rightStr;
        self.threeSecondTextField.text = contractModel.rightTwoStr;
        self.threeTextField.userInteractionEnabled = YES;
        self.threeSecondTextField.userInteractionEnabled = YES;
    } else if (contractModel.firstIndex == 2) {
        self.checkedButton = self.checkFourButton;
        self.textView.text = contractModel.rightStr;
        self.textView.userInteractionEnabled = YES;
    }
    self.checkedButton.selected = self.checkedButton ? YES : NO;
}




#pragma mark - 点击事件
- (void)checkButtonClickAction:(UIButton *)button {
    self.checkedButton.selected = NO;
    self.checkedButton = button;
    self.checkedButton.selected = !button.selected;
    self.oneTextField.userInteractionEnabled = button.tag == 100;
    self.twoTextField.userInteractionEnabled = button.tag == 101;
    self.threeTextField.userInteractionEnabled = button.tag == 102;
    self.threeSecondTextField.userInteractionEnabled = button.tag == 102;
    self.textView.userInteractionEnabled = button.tag == 103;
    if (button.tag != 100)  self.oneTextField.text = @"";
    if (button.tag != 101)  self.twoTextField.text = @"";
    if (button.tag != 102)  self.threeTextField.text = @"";
    if (button.tag != 102)  self.threeSecondTextField.text = @"";
    if (button.tag != 103)  self.textView.text = @"";
    
    self.contractModel.firstIndex = button.tag - 100 + 1;
    
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (textField == self.oneTextField) {
        self.contractModel.rightStr = textField.text;
    } else if (textField == self.twoTextField) {
        self.contractModel.rightStr = textField.text;
    } else if (textField == self.threeTextField) {
        self.contractModel.rightStr = textField.text;
    } else if (textField == self.threeSecondTextField) {
        self.contractModel.rightTwoStr = textField.text;
    }
}

#pragma mark - SISSTextViewDelegate
- (void)textViewDidEndEditing:(SISSTextView *)textView {
    
    self.contractModel.rightStr = textView.text ? textView.text : @"";
}


#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UIButton *button = [self creatButtonWithTag:100];
    [self.contentView addSubview:button];
    self.checkOneButton = button;
    
    UITextField *oneTextField = [[UITextField alloc] init];
    oneTextField.userInteractionEnabled = NO;
    self.oneTextField = oneTextField;
    NSString *oneText = @"该房地产没有设定抵押、查封或其他权利限制，卖方对该房地产享有完全的处分权，并须于本合同生效之日起";
    NSMutableAttributedString *oneAttributedText = [[NSMutableAttributedString alloc] initWithString:oneText attributes:nil];
    
    YYLabel *oneLabel = [[YYLabel alloc] init];
    oneLabel.preferredMaxLayoutWidth = (SCREEN_WIDTH - 56);
    oneLabel.numberOfLines = 0;
    oneLabel.attributedText = [self getTextAttritedStrWithAttachText:oneAttributedText strs:@[@"日内将房地产证/不动产权证原件交给居间方。"] textFields:@[oneTextField] keyboardType:UIKeyboardTypeDecimalPad placeHolders:nil];
    oneLabel.userInteractionEnabled = YES;
    [self.contentView addSubview:oneLabel];
    self.oneLabel = oneLabel;

    UIView *oneLine = [[UIView alloc] init];
    oneLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:oneLine];
    self.oneLine = oneLine;
    
    UIButton *checkTwoButton = [self creatButtonWithTag:101];
    [self.contentView addSubview:checkTwoButton];
    self.checkTwoButton = checkTwoButton;
    
    UITextField *twoTextField = [[UITextField alloc] init];
    twoTextField.userInteractionEnabled = NO;
    self.twoTextField = twoTextField;
    NSMutableAttributedString *twoAttributedText = [[NSMutableAttributedString alloc] initWithString:@"该房地产处于抵押状态，卖方承诺于本合同生效之日起" attributes:nil];
    
    YYLabel *twoLabel = [[YYLabel alloc] init];
    twoLabel.textColor = SECONDARY_TEXT_COLOR;
    twoLabel.attributedText = [self getTextAttritedStrWithAttachText:twoAttributedText strs:@[@"日内自行筹资还清贷款，办妥注销抵押登记手续，并将房地产证/不动产权证原件交给居间方。"] textFields:@[twoTextField] keyboardType:UIKeyboardTypeDecimalPad placeHolders:nil];
    twoLabel.preferredMaxLayoutWidth = (SCREEN_WIDTH - 56);
    twoLabel.numberOfLines = 0;
    twoLabel.userInteractionEnabled = YES;
    [self.contentView addSubview:twoLabel];
    self.twoLabel = twoLabel;
    
    UIView *twoLine = [[UIView alloc] init];
    twoLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:twoLine];
    self.twoLine = twoLine;
    
    UIButton *checkThreeButton = [self creatButtonWithTag:102];
    [self.contentView addSubview:checkThreeButton];
    self.checkThreeButton = checkThreeButton;
    
    UITextField *threeTextField = [[UITextField alloc] init];
    threeTextField.userInteractionEnabled = NO;
    self.threeTextField = threeTextField;
    UITextField *threeSecondTextField = [[UITextField alloc] init];
    threeSecondTextField.userInteractionEnabled = NO;
    self.threeSecondTextField = threeSecondTextField;
    NSMutableAttributedString *threeAttributedText = [[NSMutableAttributedString alloc] initWithString:@"该房地产处于抵押状态，卖方委托担保公司担保融资赎楼，须于签订本合同之日起" attributes:nil];
    
    YYLabel *threeLabel = [[YYLabel alloc] init];
    threeLabel.numberOfLines = 0;
    threeLabel.preferredMaxLayoutWidth = (SCREEN_WIDTH - 56);
    threeLabel.userInteractionEnabled= YES;
    threeLabel.attributedText = [self getTextAttritedStrWithAttachText:threeAttributedText strs:@[@"日内与担保公司签订担保服务合同、出具公证委托书，办理赎楼手续，买方应予协助，担保赎楼收费项目以担保公司的服务合同为准；卖方虽有担保公司提供担保但仍不能获得赎楼贷款的，须于本合同生效之日起", @"日内自行筹资还清贷款，办妥注销抵押登记手续，并将房地产证/不动产权证原件交给担保公司。"] textFields:@[threeTextField, threeSecondTextField] keyboardType:UIKeyboardTypeDecimalPad placeHolders:nil];
    [self.contentView addSubview:threeLabel];
    self.threeLabel = threeLabel;
    
    UIView *threeLine = [[UIView alloc] init];
    threeLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:threeLine];
    self.threeLine = threeLine;
    
    UIButton *checkFourButton = [self creatButtonWithTag:103];
    [self.contentView addSubview:checkFourButton];
    self.checkFourButton = checkFourButton;
    
    SISSTextView *textView = [[SISSTextView alloc] init];
    textView.font = [UIFont systemFontOfSize:16];
    textView.placeholder = @"其他状况";
    textView.placeholderColor = SECONDARY_TEXT_COLOR;
    textView.delegate = self;
    textView.textColor = MAIN_TEXT_COLOR;
    textView.layer.borderWidth = 1;
    textView.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    textView.userInteractionEnabled = NO;
    [self.contentView addSubview:textView];
    self.textView = textView;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;
    
    
    
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    
    [self.oneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(2 * margin);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
        make.left.equalTo(weakSelf.contentView).offset(5 * margin);
    }];
    
    [self.checkOneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(1.5 * margin);
        make.centerY.equalTo(weakSelf.oneLabel);
    }];
    [self.oneLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.oneLabel.mas_bottom).offset(margin);
        make.left.right.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
    
    [self.twoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.oneLine.mas_bottom).offset(margin);
        make.left.right.equalTo(weakSelf.oneLabel);
    }];
    [self.checkTwoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.checkOneButton);
        make.centerY.equalTo(weakSelf.twoLabel);
    }];
    [self.twoLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.twoLabel.mas_bottom).offset(margin);
        make.left.right.equalTo(weakSelf.oneLine);
        make.height.mas_equalTo(1);
    }];
    
    [self.threeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.twoLine.mas_bottom).offset(margin);
        make.left.right.equalTo(weakSelf.oneLabel);
    }];
    [self.checkThreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.checkOneButton);
        make.centerY.equalTo(weakSelf.threeLabel);
    }];
    [self.threeLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.threeLabel.mas_bottom).offset(margin);
        make.left.right.equalTo(weakSelf.oneLine);
        make.height.mas_equalTo(1);
    }];
   
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.threeLine.mas_bottom).offset(margin);
        make.left.right.equalTo(weakSelf.oneLabel);
        make.height.mas_equalTo(100);
    }];
    [self.checkFourButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.checkOneButton);
        make.centerY.equalTo(weakSelf.textView);
    }];
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.textView.mas_bottom).offset(margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}


@end
